package com.docking.heylo.service.impl;

import com.docking.heylo.ThreadContext;
import com.docking.heylo.dao.entity.*;
import com.docking.heylo.dao.mapper.BuddyRecordMapper;
import com.docking.heylo.dao.mapper.PersonalInformationMapper;
import com.docking.heylo.dao.mapper.UserBlacklistMapper;
import com.docking.heylo.enums.EUserBlack;
import com.docking.heylo.service.IBuddyService;
import com.docking.heylo.service.IPostService;
import com.docking.heylo.web.dto.PostDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class IBuddyServiceImpl implements IBuddyService {

    @Resource
    private BuddyRecordMapper buddyRecordMapper;

    @Autowired
    private IPostService postService;

    @Resource
    private PersonalInformationMapper personalInformationMapper;

    @Resource
    private UserBlacklistMapper userBlacklistMapper;



    @Override
    public List<Map<String, Object>> list(PostDto dto) {
        return buddyRecordMapper.list(ThreadContext.userId(), dto.getUserName());
    }

    /***
     * 超级喜欢
     *
     * @param userId
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void veryLike(int userId) {
        // 自己不能加自己好友
        if(userId == ThreadContext.userId()){
            return;
        }

        // 我申请的好友邀请
        BuddyRecord record = friendRequestRecord(ThreadContext.userId(), userId);
        if(record == null){
            buddyRecordMapper.insertSelective(new BuddyRecord(){{
                setUserA(ThreadContext.userId());
                setUserB(userId);
                setCrtAt(new Date());
                setAgree((byte) 0);
            }});
        }else{
            if(record.getUserA().intValue() != ThreadContext.userId()){
                buddyRecordMapper.updateByPrimaryKeySelective(new BuddyRecord(){{
                    setId(record.getId());
                    setAgree((byte) 1);
                }});
            }
        }
    }

    /***
     * 好友申请
     */
    public BuddyRecord friendRequestRecord(int meId, int friendId){
        return friend(meId, friendId);
    }

    /****
     * 好友邀请
     *
     */
    public BuddyRecord friendInvitation(int friendId, int meId){
        return friend(friendId, meId);
    }


    @Override
    public BuddyRecord friend(int userA, int userB){
        return buddyRecordMapper.friend(userA, userB);
    }

    @Override
    public void report(Integer userId) {
        userBlacklistMapper.insertSelective(new UserBlacklist(){{
            setTagId(userId);
            setUserId(ThreadContext.userId());
            setType(EUserBlack.yong_hu.getValue());
        }});
    }


    @Override
    public UserBlacklist checkReport(Integer userId) {
        UserBlacklistExample example = new UserBlacklistExample();
        UserBlacklistExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(EUserBlack.yong_hu.getValue());
        criteria.andTagIdEqualTo(userId);
        criteria.andUserIdEqualTo(ThreadContext.userId());

        List<UserBlacklist> userBlacklists = userBlacklistMapper.selectByExample(example);
        return userBlacklists != null && userBlacklists.size() > 0 ? userBlacklists.get(0) : null;
    }

    @Override
    public PageInfo<Map<String, Object>> blacks(PostDto dto) {
        PageHelper.startPage(dto.getPageNumber(), dto.getSize());
        return new PageInfo<>(userBlacklistMapper.userBlacks(dto.getUserName(), ThreadContext.userId()));
    }

    @Override
    public void removeBlack(Integer userId) {
        UserBlacklistExample example = new UserBlacklistExample();
        UserBlacklistExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(EUserBlack.yong_hu.getValue());
        criteria.andTagIdEqualTo(userId);
        criteria.andUserIdEqualTo(ThreadContext.userId());
        userBlacklistMapper.deleteByExample(example);
    }

    @Override
    public void disconnect(Post post) {
        List<Integer> userIds = new ArrayList<>();
        userIds.add(post.getUserId());
        userIds.add(ThreadContext.userId());

        BuddyRecordExample example = new BuddyRecordExample();
        BuddyRecordExample.Criteria criteria = example.createCriteria();
        criteria.andUserAIn(userIds);
        criteria.andUserBIn(userIds);
        criteria.andAgreeEqualTo((byte)1);

        int row = buddyRecordMapper.deleteByExample(example);
    }



}
